package com.atguigu.ggkt.activity.service.impl;

import com.atguigu.ggkt.activity.mapper.CouponInfoMapper;
import com.atguigu.ggkt.activity.service.CouponInfoService;
import com.atguigu.ggkt.activity.service.CouponUseService;
import com.atguigu.ggkt.model.activity.CouponInfo;
import com.atguigu.ggkt.model.activity.CouponUse;
import com.atguigu.ggkt.model.user.UserInfo;
import com.atguigu.ggkt.vo.activity.CouponUseQueryVo;
import com.atguigu.client.user.UserInfoFeignClient;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券信息 服务实现类
 * </p>
 *
 * @author Jetwang
 * @since 2022-07-02
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {
    @Autowired
    private CouponUseService couponUseService;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Override
    public void selectCouponUsePage(Page<CouponUse> pagInfo, CouponUseQueryVo couponUseQueryVo) {
        // 1.获取条件
        Long couponId = couponUseQueryVo.getCouponId();
        String couponStatus = couponUseQueryVo.getCouponStatus();
        String getTimeBegin = couponUseQueryVo.getGetTimeBegin();
        String getTimeEnd = couponUseQueryVo.getGetTimeEnd();
        // 2.封装条件查询
        LambdaQueryWrapper<CouponUse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!StringUtils.isEmpty(couponId), CouponUse::getCouponId, couponId);
        lqw.eq(!StringUtils.isEmpty(couponStatus), CouponUse::getCouponStatus, couponStatus);
        lqw.ge(!StringUtils.isEmpty(getTimeBegin), CouponUse::getGetTime, getTimeBegin);
        lqw.le(!StringUtils.isEmpty(getTimeEnd), CouponUse::getGetTime, getTimeEnd);
        // 3.分页查询
        couponUseService.page(pagInfo, lqw);
        // 4.获取数据
        List<CouponUse> couponUseList = pagInfo.getRecords();
        couponUseList.forEach(this::getUsesInfoById);
    }

    @Override
    public void updateCouponInfoUseStatus(Long couponUseId, Long orderId) {
        CouponUse couponUse = new CouponUse();
        couponUse.setId(couponUseId);
        couponUse.setOrderId(orderId);
        couponUse.setCouponStatus("1");
        couponUse.setUsingTime(new Date());
        couponUseService.updateById(couponUse);
    }

    /**
     * 获取用户信息
     * @param couponUse
     */
    private void getUsesInfoById(CouponUse couponUse) {
        // 1.获取用户id
        Long userId = couponUse.getUserId();
        if (userId == null) {
            return;
        }
        // 2.获取用户信息
        UserInfo userInfo = userInfoFeignClient.getById(userId);
        if (userInfo != null) {
            couponUse.getParam().put("nickName", userInfo.getNickName());
            couponUse.getParam().put("phone", userInfo.getPhone());
        }
    }
}
